package com.kmu.kmucampus.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.kmu.kmucampus.Fragment.AddrBookFragment;
import com.kmu.kmucampus.Fragment.AddrDetailFragment;
import com.kmu.kmucampus.Fragment.AddrDetailFragment.BackHandlerInterface;
import com.kmu.kmucampus.Fragment.NoticeFragment;
import com.kmu.kmucampus.Fragment.SettingFragment;
import com.kmu.kmucampus.R;
import com.kmu.kmucampus.adapter.MainFragmentPagerAdapter;

import java.util.ArrayList;

public class MainActivity extends KBaseActivity implements BackHandlerInterface{
    ViewPager MViewpager;
    ArrayList<Fragment> fragmentArrayList;
    private String TAG;


    private AddrDetailFragment selectedFragment;

    public static void closeStrictMode() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll().penaltyLog().build());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initDept();
        setContentView(R.layout.activity_main);
        closeStrictMode();
        InitView();
        Log.d("getRecordCount_main", "" + getFilesDir().toString());

    //    Log.d("getRecordCount", "" + st.getRecordCount(TB_STU));
    /*    sqLiteTools st= new sqLiteTools(getFilesDir().toString());
        if(st.initSelectCursor("TB_STU",null).getCount()!=0) st.cleanTB("TB_STU");*/
    }

    public void InitView() {
        MViewpager = (ViewPager) findViewById(R.id.vPager);
        Fragment fragmentLeft = new SettingFragment();
        NoticeFragment fragmentCenter = new NoticeFragment();
        AddrBookFragment fragmentRight = new AddrBookFragment();

        fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(fragmentLeft);
        fragmentArrayList.add(fragmentCenter);
        fragmentArrayList.add(fragmentRight);
        Bundle bundle = getIntent().getExtras();
        Log.d("MainActivity__InitView",""+bundle.getString("account"));
        Log.d("MainActivity__InitView",""+TB_STU);
        fragmentLeft.setArguments(bundle);
        fragmentCenter.setArguments(bundle);
        fragmentRight.setArguments(bundle);
        MViewpager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(), fragmentArrayList));
        Log.v("页面个数", "" + fragmentArrayList.size());
        MViewpager.setCurrentItem(1);
        MViewpager.setOffscreenPageLimit(3);

    }

         @Override
        public void setSelectedFragment(AddrDetailFragment backHandledFragment) {
             Log.d("setSelectedFragment","run");
                this.selectedFragment = backHandledFragment;
            }
    public void logoutBt(View v){
        SharedPreferences.Editor spEitor;
         SharedPreferences sp;
        sp = getSharedPreferences("KMUCampusLogInfo", Context.MODE_PRIVATE);
        spEitor = sp.edit();
       spEitor.putBoolean("lastLoginSucc",false);
    //   spEitor.putString("password", null);
        spEitor.putBoolean("isAutoLogin",false);
        spEitor.commit();
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        intent.putExtra("isLogout",true);    // intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
        this.finish();
    }
  /* @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {   // 是否触发按键为back键
            moveTaskToBack(true);//true对任何Activity都适用
            return true;
        } else {// 如果不是back键正常响应
   return super.onKeyDown(keyCode, event);

    }*/

    // 退出时间
    private long currentBackPressedTime = 0;

    // 退出间隔
    private static final int BACK_PRESSED_INTERVAL = 2000;
      @Override
       public void onBackPressed() {
                 Log.d("onBackPressed", "run");
                if(selectedFragment == null || !selectedFragment.onBackPressed()) {
                       super.onBackPressed();
                        moveTaskToBack(true);
                    }
        /*if (System.currentTimeMillis()- currentBackPressedTime > BACK_PRESSED_INTERVAL) {
            currentBackPressedTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();

        } else {
            // 退出
            finish();
        }*/
        //moveTaskToBack(true);
            }

}

   /*
     @Override
    protected void onNewIntent(Intent intent) {
        Log.i(TAG, "---onNewIntent---");
        super.onNewIntent(intent);
        if (intent.getBooleanExtra("isExit", false)) {
          //  finish(); // 自行关闭
        }
    }
    if (userIn == null) {
            Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "获取数据成功！" + userIn.getUserName(), Toast.LENGTH_SHORT).show();
            tv_content = (TextView) this.findViewById(R.id.cv_personalInfo);
            tv_content.setText(userIn.getUserName()+"");
        }

            public void setHandler(Handler handler) {
        mHandler = handler;
    }
            public void inter(View view) {

        Message msg = new Message();
        msg.obj = userIn.getUserName().toString();
        Log.i("userName: ", userIn.getUserName());
        msg.what = 1;
        mHandler.sendMessage(msg);
    }

        public  Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    tv_content.setText((String) msg.obj);
                    break;
            }
        };
    };


        */
// appAction.Getsomeoneinfor(account);